package org.vacoor.xqq.core.util;

import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.xvolks.jnative.JNative;   
import org.xvolks.jnative.Type;   
import org.xvolks.jnative.exceptions.NativeException;   

public class JNativeTest {
	public static void main2(String[] args)throws NativeException, IllegalAccessException, Exception {
		JNative n = null;   

		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
		calendar.add(Calendar.SECOND, 600);

        // 好像用不用-连接年月日都可以
		DateFormat f = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
		f.setTimeZone(TimeZone.getTimeZone("GMT"));
		// expires=Sun,22-Feb-2099 00:00:00 GMT
		final String expires = f.format(calendar.getTime());

		// 这里不叫InternetSetCookie了
		n = new JNative("wininet.dll", "InternetSetCookieA");
		n.setRetVal(Type.INT);   

		int i = 0;
		// url, key, value
		// 这个必须以http开头
		final String url = "https://dynamic.12306.cn/otsweb/";
		String name = "BIGipServerportal";
		String value = "3168010506.16671.0000";
		final String domain = "dynamic.12306.cn";
		final String path = "/";

		n.setParameter(i++, Type.STRING, url);
		n.setParameter(i++, Type.STRING, name);
		n.setParameter(i++, Type.STRING, value + "; Domain=" + domain + "; Path=" + path + "; expires=" + expires);   
		n.invoke();   
		System.out.println("success:" + "1".equals(n.getRetVal()));

		// ----
		name = "JSESSIONID";
		value = "6DA3E8D2E572CEBCE2C09E5244B89603";
		i = 0;
		n.setParameter(i++, Type.STRING, url);   
		n.setParameter(i++, Type.STRING, name);   
		n.setParameter(i++, Type.STRING, value + "; Domain=" + domain + "; Path=" + path + "; expires=" + expires);   
		n.invoke();
		System.out.println("success:" + "1".equals(n.getRetVal()));

		// ---
		name = "BIGipServerotsweb";
		value = "2396258570.36895.0000";
		i = 0;
		n.setParameter(i++, Type.STRING, url);   
		n.setParameter(i++, Type.STRING, name);   
		n.setParameter(i++, Type.STRING, value + "; Domain=" + domain + "; Path=" + path + "; expires=" + expires);   
		n.invoke();
		System.out.println("success:" + "1".equals(n.getRetVal()));

        final Process exec = Runtime.getRuntime().exec("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url);
        final JNative o = n;
        Thread thread = new Thread() {
            public void run() {
                try {
                    exec.waitFor();
                    int i = 0;
                    o.setParameter(i++, Type.STRING, url);
                    o.setParameter(i++, Type.STRING, "JSESSIONID");
                    o.setParameter(i++, Type.STRING, "; Domain=" + domain + "; Path=" + path + "; expires=" + expires);
                    o.invoke();
                    System.out.println("success:" + "1".equals(o.getRetVal()));

                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NativeException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (IllegalAccessException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        };
        thread.start();
        thread.join();
        System.out.println("over");
	}

    public static void main(String[] args) throws NativeException, IllegalAccessException {
        JNative n = new JNative("breakgen64.dll", "GetCurrentProcess");
        n.setRetVal(Type.LONG);
        n.invoke();
        int ret = n.getRetValAsInt();
        System.out.println(ret);
    }
}
